<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="purchaseOrderId">
        <el-input
          v-model="queryParams.purchaseOrderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否主单" prop="isMainOrder">
        <el-select
          v-model="queryParams.isMainOrder"
          placeholder="请选择是否主单"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.IS_MAIN_ORDER)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主单编号" prop="mainOrderId">
        <el-input
          v-model="queryParams.mainOrderId"
          placeholder="请输入主单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单时间" prop="orderTime">
        <el-date-picker
          v-model="queryParams.orderTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ERP_SUPPLIER_ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:pur-purchase-order:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 上传发票
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:pur-purchase-order:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" ref="tableRef"  @selection-change="handleSelectionChange">
      <el-table-column width="30" label="选择" type="selection" />
      <el-table-column label="编号" align="center" prop="id" v-if="false"/>
      <el-table-column label="订单编号" align="center" prop="purchaseOrderId" />
      <el-table-column label="一级库整合单编号" align="center" prop="integrateId" v-if="false" />
      <el-table-column label="是否主单" align="center" prop="isMainOrder">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.IS_MAIN_ORDER" :value="scope.row.isMainOrder" />
        </template>
      </el-table-column>
      <el-table-column label="主单编号" align="center" prop="mainOrderId" />
      <el-table-column
        label="订单时间"
        align="center"
        prop="orderTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="供应商编号" align="center" prop="supplierId" v-if="false"/>
      <el-table-column label="合计价格" align="center" prop="totalPrice" />
      <el-table-column label="备注" align="center" prop="remark" v-if="false"/>
      <el-table-column label="状态" align="center" prop="status" v-if="false">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ERP_SUPPLIER_ORDER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="流程实例的编号" align="center" prop="processInstanceId" v-if="false"/>
      <el-table-column label="创建者" align="center" prop="creator" v-if="false"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
        v-if="false"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PurPurchaseOrderForm1 ref="formRef" @success="getList" width = "50%"/>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { PurPurchaseOrderApi, PurPurchaseOrderVO } from '@/api/erp/purpurchaseorder'
import PurPurchaseOrderForm1 from './PurPurchaseOrderForm1.vue'

/** ERP 采购单 列表 */
defineOptions({ name: 'PurPurchaseOrder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PurPurchaseOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  purchaseOrderId: undefined,
  integrateId: undefined,
  isMainOrder: undefined,
  isFinished: 1,
  mainOrderId: undefined,
  orderTime: [],
  supplierId: undefined,
  totalPrice: undefined,
  remark: undefined,
  status: undefined,
  processInstanceId: undefined,
  creator: undefined,
  createTime: [],
  orderIntegrateId: '0'
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PurPurchaseOrderApi.getPurPurchaseOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string) => {
  if(selectionList.value.length === 0){
    message.warning(t('common.invoiceUpload'))
    return
  }
  formRef.value.open(type, selectionList)
}


/** 选中操作 */
const selectionList = ref<PurPurchaseOrderVO[]>([])
const handleSelectionChange = (rows: PurPurchaseOrderVO[]) => {
  selectionList.value = rows
}


/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PurPurchaseOrderApi.exportPurPurchaseOrder(queryParams)
    download.excel(data, 'ERP 采购单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>